class FlashcardsController < ApplicationController
 
  before_filter :clear_game
  before_filter :load_fset, :except => [:destroy, :edit_ajax, :delete_ajax]

  def load_fset
    @fset = Fset.find(params[:fset_id])
  end	
 # GET /flashcards
  # GET /flashcards.xml
  def index
    @flashcards = Flashcard.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @flashcards }
    end
  end

  # GET /flashcards/1
  # GET /flashcards/1.xml
  def show
    @flashcard = Flashcard.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @flashcard }
    end
  end

  # GET /flashcards/new
  # GET /flashcards/new.xml
  def new
    @flashcard = Flashcard.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @flashcard }
    end
  end

  # GET /flashcards/1/edit
  def edit
    @flashcard = Flashcard.find(params[:id])
  end

  # GET /flashcards/1/edit
  def edit_ajax
    @flashcard = Flashcard.find(params[:id])
    @fset = @flashcard.fset
#    @fset = @flashcard.fset
    
    respond_to do |format|
      format.js { render :partial => 'edit_part' }
#      render :text => "bob"
    end
  end


  # POST /flashcards
  # POST /flashcards.xml
  def create
#	@event = Event.find(params[:event_id])
#   @comment =  Comment.new(params[:comment])
#   @comment.event = @event
  
    @flashcard = Flashcard.new(params[:flashcard])
    @flashcard.fset = @fset
    @new_flashcard = Flashcard.new
	#    @flashcard.fset_id = Fset.find(params[:fset_id])

    respond_to do |format|
      if @flashcard.save
        flash[:notice] = 'Flashcard was successfully created.'
        format.html { redirect_to(fset_path(@fset)) }
        format.xml  { render :xml => @flashcard, :status => :created, :location => @flashcard }
        format.js   { render :action => 'new'}
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @flashcard.errors, :status => :unprocessable_entity }
        format.js   { render :action => 'new'}
      end
    end
  end

  # PUT /flashcards/1
  # PUT /flashcards/1.xml
  def update
    @flashcard = Flashcard.find(params[:id])
    @fset = @flashcard.fset

    respond_to do |format|
      if @flashcard.update_attributes(params[:flashcard])
        flash[:notice] = 'Flashcard was successfully updated.'
        format.html { redirect_to(edit_fset_path(@fset)) }
        format.xml  { head :ok }
        format.js { render :action => 'show' }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @flashcard.errors, :status => :unprocessable_entity }
        format.js { render :partial => 'edit_part' }
      end
    end
  end

  # DELETE /flashcards/1
  # DELETE /flashcards/1.xml
  def destroy
    @flashcard = Flashcard.find(params[:id])
	  @fset = @flashcard.fset
    @flashcard.destroy
    respond_to do |format|
      format.html { redirect_to(edit_fset_path(@fset)) }
      format.xml  { head :ok }
    end
  end
  def delete_ajax
    @flashcard = Flashcard.find(params[:id])
	  @fset = @flashcard.fset
    @flashcard.destroy
    respond_to do |format|
      format.js { render :text => "None" }
      format.xml  { head :ok }
    end
  end


end
